<template>
  <div>
    <jiazai />
    <div class="register-box">
      <h2>注册</h2>
      <form @submit.prevent>
        <div class="form-control">
          <label for="username">用户名</label>
          <input
            type="text"
            v-model="username"
            id="username"
            @input="validateUsername"
          />
          <span class="error-message">{{
            usernameError
          }}</span>
        </div>
        <div class="form-control">
          <label for="password">密码</label>
          <input
            type="password"
            v-model="password"
            id="password"
            @input="validatePassword"
          />
          <span class="error-message">{{
            passwordError
          }}</span>
        </div>
        <div class="form-control">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            v-model="confirmPassword"
            id="confirmPassword"
            @input="validateConfirmPassword"
          />
          <span class="error-message">{{
            confirmPasswordError
          }}</span>
        </div>
        <div class="form-control">
          <button type="submit" @click="register" class="half-width">
            注册
          </button>
          <button type="button" @click="goToLogin" class="half-width">
            返回登录
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
    <script>
import jiazai from "../zi/加载中.vue";
import { provide, ref, computed, inject } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

export default {
  components: { jiazai },
  setup() {
    const store = inject("store");
    const count1 = computed(() => store.state.zhanghui);
    const isLoading = ref(false);
    provide("isLoading", isLoading);
    const router = useRouter();

    const username = ref("");
    const password = ref("");
    const confirmPassword = ref("");

    const usernameError = ref("");
    const passwordError = ref("");
    const confirmPasswordError = ref("");

    function validateUsername() {
      if (username.value.trim().length < 3) {
        usernameError.value = "用户名长度不能少于3位";
      } else {
        usernameError.value = "";
      }
    }

    function validatePassword() {
      if (password.value.trim().length < 4) {
        passwordError.value = "密码长度不能少于4位";
      } else {
        passwordError.value = "";
      }
    }

    function validateConfirmPassword() {
      if (confirmPassword.value.trim() !== password.value.trim()) {
        confirmPasswordError.value = "两次密码输入不一致";
      } else {
        confirmPasswordError.value = "";
      }
    }

    function register() {
      const user = count1.value.find((u) => u.username == username.value );
  
        if (
          username.value.trim() !== "" &&
          password.value.trim() !== "" &&
          confirmPassword.value.trim() !== "" &&
          usernameError.value === "" &&
          passwordError.value === "" &&
          confirmPasswordError.value === "" &&
          user === undefined
        ) {
          isLoading.value = true;
          const timerId = setTimeout(() => {
            ElMessage({
              type: "success",
              message: "注册成功，请登录",
            });
            isLoading.value = false;
            let zhanghui={username:username.value,password:password.value}
            store.commit('tianjiazhanghui',zhanghui)
            router.push("/login");
          }, 1000);
        } else {
          ElMessage({
            type: "error",
            message: "填写注册信息错误或者账户已存在",
          });
        }
      
    }

    function goToLogin() {
      isLoading.value = true;
      const timerId = setTimeout(() => {
        ElMessage({
          type: "success",
          message: "已返回登录页",
        });
        isLoading.value = false;
        router.push("/login");
        
      }, 1000);
    }

    return {
      username,
      password,
      confirmPassword,
      usernameError,
      passwordError,
      confirmPasswordError,
      validateUsername,
      validatePassword,
      validateConfirmPassword,
      register,
      goToLogin,
    };
  },
};
</script>
    
<style scoped>
input:focus {
  border-color: #8dbdff;
  box-shadow: 0 0 0 2px #8dbdff;
}
.register-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  padding: 40px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
  z-index: 999;
}

.register-box h2 {
  text-align: center;
  margin-bottom: 30px;
}

.form-control {
  margin-bottom: 20px;
}

.form-control label {
  display: block;
  margin-bottom: 5px;
}

.form-control input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  outline: none;
}

.error-message {
  height: 10px;
  color: red;
  font-size: 12px;
}

.form-control {
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
}

.half-width {
  width: 50%;
}
button[type="button"],
button[type="submit"] {
  width: 50%;
  padding: 10px;
  background-color: #fff;
  color: #161414;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
}
button[type="submit"]:hover,
button[type="button"]:hover {
  background-color: hsl(180, 80%, 70%);
}
</style>